package com.example.ice.fittime;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ice.fittime.fragment.CourseFragment;
import com.example.ice.fittime.fragment.HomeFragment;
import com.example.ice.fittime.fragment.MineFragment;
import com.example.ice.fittime.fragment.SocialFragment;

public class MainActivity extends AppCompatActivity
        implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {

    private Fragment currentFragment;
    private FragmentManager manager;
    private HomeFragment homeFragment;
    private CourseFragment courseFragment;
    private SocialFragment socialFragment;
    private MineFragment mineFragment;
    private ImageView bar_left;
    private TextView bar_title;
    private ImageView bar_right_iv;
    private TextView bar_right_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //我们的代码
        initContentMainView();

    }

    private void initFragment() {
//        FrameLayout container = (FrameLayout) findViewById(R.id.main_container);
        homeFragment = new HomeFragment();
        courseFragment = new CourseFragment();
        socialFragment = new SocialFragment();
        mineFragment = new MineFragment();
        manager = getSupportFragmentManager();
        manager.beginTransaction()
                .add(R.id.main_container, homeFragment)
                .add(R.id.main_container, courseFragment)
                .add(R.id.main_container, socialFragment)
                .add(R.id.main_container, mineFragment)
                .hide(homeFragment)
                .hide(courseFragment)
                .hide(socialFragment)
                .hide(mineFragment).commit();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction transaction = manager.beginTransaction();
        switch (checkedId) {
            case R.id.rb_home:
                transaction.hide(currentFragment).show(homeFragment);
//                bar_left.setVisibility(View.INVISIBLE);
//                bar_right_tv.setVisibility(View.INVISIBLE);
//                bar_right_iv.setVisibility(View.INVISIBLE);
//                bar_title.setText("FIT TIME");
                break;
            case R.id.rb_course:
                transaction.hide(currentFragment).show(courseFragment);
//                bar_left.setVisibility(View.INVISIBLE);
//                bar_right_tv.setVisibility(View.INVISIBLE);
//                bar_right_iv.setVisibility(View.VISIBLE);
//                bar_title.setText("课程");
//                bar_right_iv.setImageResource(R.mipmap.ic_course_market_home);
                break;
            case R.id.rb_social:
                transaction.hide(currentFragment).show(socialFragment);
//                bar_left.setVisibility(View.GONE);
//                bar_right_tv.setVisibility(View.GONE);
//                bar_right_iv.setVisibility(View.GONE);
//                bar_title.setVisibility(View.GONE);
                break;
            case R.id.rb_mine:
                transaction.hide(currentFragment).show(mineFragment);
//                bar_left.setVisibility(View.INVISIBLE);
//                bar_right_tv.setVisibility(View.VISIBLE);
//                bar_right_iv.setVisibility(View.INVISIBLE);
//                bar_title.setText("用户名");
//                bar_right_tv.setText("设置");
                break;
        }
        transaction.commit();
    }

    private void initContentMainView() {
        RadioGroup rg = (RadioGroup) findViewById(R.id.main_rg);
        rg.setOnCheckedChangeListener(this);
        initFragment();
        initAppBar();
        initView();
        initClick();
    }

    private void initClick() {
//        bar_right_tv.setOnClickListener(this);
//        bar_right_iv.setOnClickListener(this);
//        bar_left.setOnClickListener(this);
    }

    private void initView() {
        currentFragment = homeFragment;
        manager.beginTransaction().show(homeFragment).commit();
//        bar_title.setText("FIT TIME");
    }

    private void initAppBar() {
//        bar_left = (ImageView) findViewById(R.id.bar_left);
//        bar_title = (TextView) findViewById(R.id.bar_title);
//        bar_right_iv = (ImageView) findViewById(R.id.bar_right_iv);
//        bar_right_tv = (TextView) findViewById(R.id.bar_right_tv);
    }

    // TODO: 2016/10/12 在这里写appBar的点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bar_left:
                Toast.makeText(this, "左图标被点击了", Toast.LENGTH_SHORT).show();
                break;
            case R.id.bar_right_iv:
                Toast.makeText(this, "右图标被点击了", Toast.LENGTH_SHORT).show();
                break;
            case R.id.bar_right_tv:
                Toast.makeText(this, "右文本被点击了", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
